package com.ruoyi.web.util;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.Iterator;

public class ClientUtil {
    /**
     * 调用WEBService接口
     * @param wsdl
     * @param functionName
     * @param param1
     * @return
     * @throws Exception
     */
    public static  String contextLoads(String wsdl ,String functionName,String param1) throws Exception {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        //回调接口测试地址
        org.apache.cxf.endpoint.Client client = dcf.createClient(wsdl);
        Object[] objects = client.invoke(functionName,param1);//list3方法名 后面是可变参数
        //输出调用结果
        return objects[0].toString();
    }
    public static Record xml2Record(String XmlStr)
    {
        Record data=new Record();
        Document doc = null;
        try {
            doc = DocumentHelper.parseText(XmlStr);
        } catch (Exception e) {
            e.printStackTrace();
            return data.set("erroCode","99");
        }
        Element root = doc.getRootElement();// 指向根节点
        Iterator it = root.elementIterator();
        while (it.hasNext()) {
            //遍历处理数据入库
            Element element = (Element) it.next();// 一个Item节点
            if(StrKit.notBlank(element.getTextTrim()))
            {
                data.set(element.getName(),element.getTextTrim());
            }
            //System.out.println(element.getName() + " : " + element.getTextTrim());
        }
        return data;
    }
}
